#encoding=utf8
'''
Created on 2012-10-18

@author: Administrator
'''
from Processor import Processor
from models.UserModel import UserModel
from pub import Func, UserDict
from pub.Config import Config


class RINUClass(Processor):
    '''
    指令：RINU(room idiom near users) 
    备注：附近游戏玩家列表
    格式：c-s:cmd #cmdID#userID#longitude#latitude
     s-c:cmd #cmdID#userID# userNickName #userPohoto
    '''


    def __init__(self):
        Processor.__init__(self);
        self.__userId = 0;
        self.__longitude = 0;#经度
        self.__latitude = 0;#纬度
        
    def dataValidtor(self, cmdContent):
        if not cmdContent:
            return False;
        paraLen = len(cmdContent);
        if paraLen == 3:      
            self.__userId = Func.strToInt(cmdContent[0], Config.I_SYS_ZERO);
            self.__longitude = Func.toFloat(cmdContent[1], Config.I_SYS_ZERO);
            self.__latitude = Func.toFloat(cmdContent[2], Config.I_SYS_ZERO);            
            return True;        
        return False;
    def outData(self,):
        userM=UserModel();
        user=userM.getUserFriendsById(self.__userId);
        if user:
            user.set_user_login_pos_x(self.__longitude);
            user.set_user_login_pos_y(self.__latitude);
            userM.modifanInfo(user);
        
        
        
        onLineUserList = UserDict.getAllOnlineUser();
        sendData = [];
        if not onLineUserList:
            self._sockObj.sendLine(self.setPackage(sendData));    
            return True;
        
        
        for user in onLineUserList:
            long = user.get_user_login_pos_x()#经度
            lat = user.get_user_login_pos_y();#纬度
            distance = Func.spherical_distance((self.__longitude, self.__latitude), (long, lat));
            if not user.get_room() and distance <= Config.I_MAX_DISTANCE:
                sendData.append(user.get_user_id());
                sendData.append(user.get_user_nick_name());
                sendData.append(user.get_user_photo());
                sendData.append(distance);
        sendStr = self.setPackage(sendData);
        Func.sendMsgStr(self._sockObj, sendStr);
        return True;            
